<#assign content>
<div class="right_col" role="main">
    <div class="">
        <div class="page-title">
            <div class="title_left">
                <h3>角色列表</h3>
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <div class="x_panel">
                        <div class="x_title">
                            <h2><small></small></h2>
                            <ul class="nav navbar-right panel_toolbox">
                                <#if RoleActionTag.roleActionCode(104)>
                                    <li><a id="roleadd"><i class="fa fa-plus-circle"></i></a></li>
                                </#if>
                                <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                                </li>
                            </ul>
                            <div class="clearfix"></div>
                        </div>
                        <div class="x_content">
                            <p class="text-muted font-13 m-b-30">
                            </p>
                            <table id="roletable" class="table table-striped table-bordered">
                                <thead>
                                <tr>
                                    <th>角色名称</th>
                                    <th>角色描述</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>

                                <tfoot>
                                <tr>
                                    <th>角色名称</th>
                                    <th>角色描述</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</#assign>
<#include "../_inc/layout.ftl" />
// 角色新增
<div class="modal fade" id="roleAddModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">角色新增</h4>
            </div>
            <form id="roleAddForm" action="/role_add.do" method="post" novalidate class="form-horizontal form-label-left">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">角色名称<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="rolename" name="rolename" placeholder="请输入角色名称" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-3">角色描述<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="rolememo" name="rolememo" required="required" placeholder="请输入角色描述" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saverole" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

// 角色修改
<div class="modal fade" id="roleEditModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">角色修改</h4>
            </div>
            <form id="roleEditForm" action="/role_edit.do" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="edit_roleId" name="id">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">角色名称<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="edit_rolename" name="rolename" placeholder="请输入角色名称" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-3">角色描述<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="edit_rolememo" name="rolemome" required="required" placeholder="请输入角色描述" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="save_editrole" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

// 删除角色
<div class="modal fade" id="roleDeleteModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">角色删除</h4>
            </div>
            <form id="roleDeleteForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="delete_roleId"  name="id">
                <div class="modal-body">
                    <div class="item form-group">
                        <div class="col-md-10">
                            <span id="deleteRoleMessage"></span>
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="save_deleterole" type="button" class="btn btn-success">确定</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<script>
    var datatable;

    $(document).ready(function() {
        datatable = $('#roletable').DataTable({
            lengthChange: false,            // 是否允许用户自定义显示数量
            bPaginate: true,                // 翻页功能
            bFilter: false,                 // 列筛序功能
            ordering: false,                // 排序功能
            Info: true,                     // 页脚信息
            autoWidth: true,                // 自动宽度
            pageLength: 10,                 // 每页显示10条数据
            oLanguage: {                    // 国际语言转化
                oAria: {
                    sSortAscending: ': 以升序排列此列',
                    sSortDescending: ': 以降序排列此列'
                },
                sLengthMenu: '显示 _MENU_ 记录',
                sZeroRecords: '对不起，查询不到任何相关数据',
                sEmptyTable: '未有相关数据',
                sLoadingRecords: '正在加载数据...',
                sInfo: '当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录。',
                sInfoEmpty: '当前显示0到0条，共0条记录。',
                sSearch: '查询:',
                oPaginate: {
                    sFirst: '首页',
                    sPrevious: ' 上一页 ',
                    sNext: ' 下一页 ',
                    sLast: ' 尾页 '
                }
            },
            stripeClasses: ["odd", "even"],                         //为奇偶行加上样式，兼容不支持CSS伪类的场合
            paginationType: 'full_numbers',
            serverSide: true,
            ajax: function (data, callback, settings) {
                var param = {};                                     // 封装请求参数
                param.limit = data.length;                          // 页面显示记录条数，在页面显示每页显示多少项的时候
                param.start = data.start;                           // 开始的记录序号
                param.page = (data.start / data.length) + 1;        // 当前页码
                param.action = 'page';
                $.ajax({
                    type: 'GET',
                    url: '/role_list.do',
                    cache: false,                                   //禁用缓存
                    data: param,                                    //传入组装的参数
                    dataType: 'json',
                    success: function (result) {
                        // 封装返回数据
                        var returnData = {};
                        returnData.recordsTotal = result['recordsTotal'];              // 返回数据全部记录
                        returnData.recordsFiltered = result['recordsFiltered'];        // 后台不实现过滤功能，每次查询均视作全部结果
                        returnData.data = result['records'];                           // 返回的数据列表
                        callback(returnData);
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            },
            columns: [
                { data: 'roleName' },
                { data: 'roleMemo' },
                { data: 'createTime',
                    render: function(data, type, row, meta){
                        return $.jsonDateFormat(data, 'yyyy-MM-dd hh:mm:ss');
                    }
                },
                {
                    data: 'id',
                    render: function (data, type, row, meta) {
                        var str = '';
                        <#if RoleActionTag.roleActionCode(107)>
                            str += '<button onclick="$.editRole(' + data + ')" type="button" class="btn btn-success btn-xs"><i class="fa fa-pencil"></i>修改</button>';
                        </#if>
                        <#if RoleActionTag.roleActionCode(108)>
                            str += '<button onclick="$.deleteRole(' + data + ',\'' + row.roleName + '\')" type="button" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>删除</button>';
                        </#if>
                        <#if RoleActionTag.roleActionCode(102)>
                            str += '<button onclick="$.roleSetMenu(' + data + ')" type="button" class="btn btn-info btn-xs"><i class="fa fa-list-ul"></i>设置菜单</button>';
                        </#if>
                        <#if RoleActionTag.roleActionCode(101)>
                            str += '<button onclick="$.roleSetAction(' + data + ')" type="button" class="btn btn-info btn-xs"><i class="fa fa-columns"></i>设置功能</button>';
                        </#if>
                        return str;
                    }
                }
            ],
            columnDefs: [
                { orderable: false, targets: '_all' },
                { defaultContent: '', targets: '_all'}
            ]
        } );

        //
        $('#roleadd').on('click', function () {
            var param = {};
            param.action = 'open';
            $('#roleAddModal').modal({
                keyboard: true
            });
        });

        // 窗体关闭清空页面
        $('#roleAddModal').on('hide.bs.modal', function () {
            $('#roleAddForm')[0].reset();
        });

        // 提交保存
        $('#saverole').on('click', function () {
            if(validator.checkAll($('#roleAddForm'))) {
                var param = {};
                param.action = 'do';
                param.rolename = $('#rolename').val();
                param.rolememo = $('#rolememo').val();
                $('#roleAddForm')[0].reset();
                $('#roleAddModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/role_add.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function(result) {
                        if(result.success) {
                            datatable.draw(false);
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });
        //
        $.editRole = function (id) {
            var param = {};
            param.action = 'edit';
            param.id = id;
            $.ajax({
                type: 'POST',
                url: '/role_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function(result) {
                    if(result.success) {
                        $('#edit_roleId').val(result.body.sysRole.id);
                        $('#edit_rolename').val(result.body.sysRole.roleName);
                        $('#edit_rolememo').val(result.body.sysRole.roleMemo);
                        $('#roleEditModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $('#roleEditForm')[0].reset();
                    $('#roleEditModal').modal('hide');
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        };
        // 窗体关闭清空页面
        $('#roleEditModal').on('hide.bs.modal', function () {
            $('#roleEditForm')[0].reset();
        });
        // 保存修改
        $('#save_editrole').on('click', function () {
            if(validator.checkAll($('#roleEditForm'))) {
                var param = {};
                param.action = 'doEdit';
                param.id = $('#edit_roleId').val();
                param.rolename = $('#edit_rolename').val();
                param.rolememo = $('#edit_rolememo').val();
                $('#roleEditForm')[0].reset();
                $('#roleEditModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/role_edit.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function(result) {
                        if(result.success) {
                            datatable.draw(false);
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });
        //
        $.deleteRole = function (id, rolename) {
            $('#delete_roleId').val(id);
            $('#deleteRoleMessage').html('确定删除' + rolename + '角色信息，一旦删除将影响关联用户使用系统功能？');
            $('#roleDeleteModal').modal({
                keyboard: true
            });
        };
        $('#save_deleterole').on('click', function () {
            var param = {};
            param.action = 'doDelete';
            param.id = $('#delete_roleId').val();
            $('#roleDeleteModal').modal('hide');
            $.ajax({
                type: 'POST',
                url: '/role_delete.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function(result) {
                    if(result.success) {
                        datatable.draw(false);
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        });
        //
        $.roleSetMenu = function (id) {
            var param = {};
            param.action = 'list';
            param.id = id;
            $.submitFormPost('/role_set_menu.do', param, 'POST');
        };
        $.roleSetAction = function (id) {
            var param = {};
            param.action = 'list';
            param.id = id;
            $.submitFormPost('/role_set_action.do', param, 'POST');
        };
    } );

</script>